/**
 * Created by rockyren on 14/11/29.
 */
describe('购物车路由测试',function(){
  beforeEach(module('cartApp'));

  var $location,$route,$rootScope,$httpBackend;
  beforeEach(inject(function(_$location_,_$route_,_$rootScope_,_$httpBackend_){
    $location = _$location_;
    $route = _$route_;
    $rootScope = _$rootScope_;
    $httpBackend = _$httpBackend_;
  }));

  /**
   * @method 测试特定路由是否被加载,及其对应的控制器
   * @param expectTpl 预期的html页面
   * @param targetUrl 要测试的路由
   * @param targetController 路由对应的控制器
   */
  function checkRoute(expectTpl,targetUrl,targetController) {
    var tplsUrl = 'ngApp/cart/tpls/';
    var expectUrl = tplsUrl + expectTpl;
    $httpBackend.expectGET(expectUrl).respond(200);
    $location.path(targetUrl);
    $rootScope.$digest();
    expect($route.current.controller).toBe(targetController);
    expect($route.current.controller).not.toBe('ctrl');

    $httpBackend.flush();
  }

  describe('index route',function(){
    it('should load the cart page on successful load of /',function(){
      checkRoute('cart-index.html','/','cartIndexController');
    });

    it('should redirect to the index path on non-existent route',function(){
      checkRoute('cart-index.html', 'not/a/route', 'cartIndexController');
    });
  });



});